import numpy as np
import pandas as pd
from collections import Counter


def ind_cnt(yi, xi):
    xi[xi < 0] = 0
    xi[xi >= 330] = 329
    yi[yi < 0] = 0
    yi[yi >= 110] = 109
    M = Counter(zip(yi, xi))
    return M


train = pd.read_hdf('../input/test.h5')
arr = []
ships = train.loc[:, 'ship'].unique()
for i in range(len(ships)):
    I = np.zeros((110, 330))
    ship = ships[i]
    data0 = train.loc[(train['ship'] == ship), :]
    lon, lat = data0['y'] / 50000, data0['x'] / 200000
    xi = ((lon - 90) / 0.1).astype(np.int)
    yi = ((lat - 25) / 0.1).astype(np.int)
    W = ind_cnt(yi, xi)
    print(i, ': 停驻位置个数', len(W), ',', 'max:', max(W.values()))
    xi1, yi1 = np.array(list(W.keys()))[:, 0], np.array(list(W.keys()))[:, 1]
    I[xi1, yi1] = list(W.values())
    I1 = I/np.max(I)*255
    I2 = I1.astype(np.uint8)
    arr += [I2.reshape(-1)]

Arr = np.array(arr)
res = pd.DataFrame(Arr)
res['ship'] = train.drop_duplicates(['ship']).reset_index()['ship']
res.to_hdf('../data/im1x3_tst.h5', 'df', mode='w')


